

_realname=rustup
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.28.2
pkgrel=1
pkgdesc="The Rust toolchain installer (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/rust-lang/rustup"
license=('spdx:Apache-2.0 AND MIT')
msys2_references=(
  'archlinux: rustup'
)
depends=(
  "${MINGW_PACKAGE_PREFIX}-curl"
  "${MINGW_PACKAGE_PREFIX}-xz"
  "${MINGW_PACKAGE_PREFIX}-zstd"
)
makedepends=(
  "git"
  "${MINGW_PACKAGE_PREFIX}-rust"
  "${MINGW_PACKAGE_PREFIX}-rust-bindgen"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-nasm"
)
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-gdb: rust-gdb script"
  "${MINGW_PACKAGE_PREFIX}-lldb: rust-lldb script"
)
provides=("${MINGW_PACKAGE_PREFIX}-rust")
conflicts=("${MINGW_PACKAGE_PREFIX}-rust")
install='post.install'
options=("!lto")
source=(
  "${url}/archive/${pkgver}/rustup-${pkgver}.tar.gz"
  "rustup-profile.sh"
)
sha256sums=('5987dcb828068a4a5e29ba99ab26f2983ac0c6e2e4dc3e5b3a3c0fafb69abbc0'
            'dec8fd8b2838e7e5866a0bfbae2be89647c7c70a46c0ada1406accf4017322e9')
_binlinks=('cargo' 'rustc' 'rustdoc' 'rust-gdb' 'rust-lldb' 'rustfmt' 'cargo-fmt' 'cargo-clippy' 'clippy-driver' 'cargo-miri')

prepare() {
  cd "${_realname}-${pkgver}"

  cargo update -p aws-lc-rs --precise 1.13.1
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}-${pkgver}"

  cargo build --release --frozen --features no-self-update --bin rustup-init
}

package() {
  cd "${_realname}-${pkgver}"

  install -d "${pkgdir}${MINGW_PREFIX}/lib/${_realname}/bin"
  install -Dm755 "target/release/rustup-init" "${pkgdir}${MINGW_PREFIX}/bin/rustup.exe"
  for link in "${_binlinks[@]}"; do
    ln "${pkgdir}${MINGW_PREFIX}/bin/rustup.exe" "${pkgdir}${MINGW_PREFIX}/bin/${link}.exe"
  done

  # Special treatment to allow for rust-analyzer to still allow the separate package version to be used.
  ln "${pkgdir}${MINGW_PREFIX}/bin/rustup.exe" "${pkgdir}${MINGW_PREFIX}/lib/${_realname}/bin/rust-analyzer.exe"

  install -Dm644 "${srcdir}/rustup-profile.sh" "${pkgdir}${MINGW_PREFIX}/etc/profile.d/rustup.sh"

  # Generate completion files.
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions bash > "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/rustup"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions bash cargo > "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/cargo"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions fish > "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/rustup.fish"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions zsh > "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_rustup"
  "${pkgdir}${MINGW_PREFIX}/bin/rustup" completions zsh cargo > "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_cargo"

  install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/${pkgname}/LICENSE-MIT"
  install -Dm644 LICENSE-APACHE "${pkgdir}${MINGW_PREFIX}/share/licenses/${pkgname}/LICENSE-APACHE"
}
